
package com.hyst.seviceImpl;

import com.hyst.domain.PurchaseCourse;
import com.hyst.domain.RecommendMsgCustomer;
import com.hyst.repository.PurchaseCourseMapper;
import com.hyst.service.CurriculumScheduleService;
import com.hyst.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.RecommendMsg;
import com.hyst.domain.StudentView;
import com.hyst.repository.RecommendMsgMapper;
import com.hyst.service.RecommendMsgService;
import com.hyst.utils.UuidTool;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Date：2018年09月26日 
 * @author： dy 
 * @version ：1.0 1.[2018年09月26日]
 */
@Service
public class RecommendMsgServiceImpl implements RecommendMsgService {

	@Autowired
	private RecommendMsgMapper mapper;

	@Autowired
	private CurriculumScheduleService curriculumScheduleService;

	@Autowired
	private PurchaseCourseMapper purchaseCourseMapper;

	@Override
	public Page<RecommendMsg> list(final RecommendMsg recommendMsg) {
		final Page<RecommendMsg> page = PageHelper.startPage(recommendMsg.getPageNum(), recommendMsg.getPageSize());
		List<RecommendMsg> select = mapper.select(recommendMsg);
//		List<RecommendMsg> select = mapper.getAll(recommendMsg.getRecommenderId());

		for(RecommendMsg msg:select){
			//注册状态
			Integer status = mapper.getStatus(msg.getNewStudentId());
			if(status>0){
				msg.setRegStatus("已注册");
			}else{
				msg.setRegStatus("未注册");
			}

			//试听状态
			List<String> statueFree=mapper.getStausfree(msg.getNewStudentId());
			if(statueFree.size()!=0 && statueFree.contains("已完成")){
				msg.setLisStatus("已试听");
			}else {
				msg.setLisStatus("未试听");
			}


			//付款状态
			List<String> statusFukuan = mapper.getStatusFukuan(msg.getNewStudentId());
			if(statusFukuan.size()!=0 && statusFukuan.contains("已支付")){
				msg.setFeeStatus("已支付");
			}else{
				msg.setFeeStatus("未支付");
			}
		}
		return page;
	}

	@Override
	public RecommendMsg insert(final RecommendMsg recommendMsg) {
		recommendMsg.setUniqueId(UuidTool.randomUUID());
		mapper.insertSelective(recommendMsg);
		return recommendMsg;
	}

	@Override
	public RecommendMsg getByPriKey(final String uniqueId) {
		final RecommendMsg recommendMsg = mapper.selectByPrimaryKey(uniqueId);
		return recommendMsg;
	}

	@Override
	public RecommendMsg updateSelective(final RecommendMsg recommendMsg) {
		mapper.updateByPrimaryKeySelective(recommendMsg);
		return recommendMsg;
	}

	@Override
	public RecommendMsg update(final RecommendMsg recommendMsg) {
		mapper.updateByPrimaryKey(recommendMsg);
		return recommendMsg;
	}

	@Override
	public RecommendMsg delete(final String uniqueId) {

		return null;
	}
	
	// 2018-9-27 chenbin: 奖励发放和备注
	@Transactional
	public RecommendMsg saveAward(String saveMsg) {
		try {
			String []a=saveMsg.split("&");
			RecommendMsg message = new RecommendMsg();
			message.setUniqueId(a[0]);
			message.setRecommendAward(a[1]);
			message.setRemark(a[2]);

			RecommendMsg recommendMsg = mapper.selectByPrimaryKey(message);
			PurchaseCourse purchaseCourse = purchaseCourseMapper.getByStudent(recommendMsg.getRecommenderId());
			if(recommendMsg !=null && org.apache.commons.lang3.StringUtils.isNotBlank(recommendMsg.getRecommenderId()) && purchaseCourse !=null){
				//增加考勤
				curriculumScheduleService.addLevalClass(purchaseCourse.getUniqueid(),Integer.valueOf(a[1]),null);

				//增加总时长
				Integer hoursEnrolled = purchaseCourse.getHoursEnrolled();
				int hourNew = hoursEnrolled + Integer.valueOf(a[1]);
				PurchaseCourse purchaseCourse1 = new PurchaseCourse();
				purchaseCourse1.setUniqueid(purchaseCourse.getUniqueid());
				purchaseCourse1.setHoursEnrolled(hourNew);
				purchaseCourseMapper.updateHour(purchaseCourse1);

				recommendMsg.setJiangStatus("已发放");
				mapper.saveStatus(recommendMsg);
			}
//			mapper.saveAwardData(message);
		} catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	// 2018-9-27 chenbin: 奖励核销和备注
	public RecommendMsg saveSubstract(String saveMsg) {
		String []a=saveMsg.split("&");
		RecommendMsg message = new RecommendMsg();
		message.setUniqueId(a[0]);
		message.setSubstractAward(a[1]);
		message.setRemark(a[2]);
		mapper.saveSubstractData(message);
		return null;
	}

}
